import Toast from "../../app/popups/Toast";
import LocalStorage from "./LocalStorage";

export default class DebugUtils {
    public static _debugTag: string = "1486940";

    public static _debug: boolean = CC_DEV;

    public static showBound(node: cc.Node, fill: boolean = true) {
        if (!this._debug) {
            return;
        }
        let g = node.getComponent(cc.Graphics);
        let chNode = null;
        if (g) {
            // 如果原节点上有graphic, 不能影响原节点. 需要重新建一层节点..
            chNode = node.getChildByName(DebugUtils._debugTag);

            // 单独加一个调试节点,用于显示一些额外信息.
            if (chNode == null) {
                chNode = new cc.Node("debugNode");
                node.addChild(chNode, 100000, DebugUtils._debugTag);
            }

            chNode.width = node.width;
            chNode.height = node.height;
            chNode.setAnchorPoint(node.getAnchorPoint());
            chNode.setPosition(0, 0);

            g = chNode.getComponent(cc.Graphics);
            if (g == null) {
                g = chNode.addComponent(cc.Graphics);
            }
        } else {
            g = node.addComponent(cc.Graphics);
        }
        if (!g) {
            return;
        }

        g.lineWidth = 2;
        g.strokeColor = cc.Color.RED;
        g.fillColor = cc.color(0xff, 0, 0, 50);
        const rect = node.getBoundingBox();

        if (fill) {
            g.fillRect(0, 0, rect.width, rect.height);
            g.fill();
        }
        g.circle(node.anchorX * node.width, node.anchorY * node.height, 5);
        g.rect(0, 0, rect.width, rect.height);
        g.stroke();
    }

    public static clearStorage() {
        if (!this._debug) {
            Toast.showToast("非开发版本,正在清除本地数据,请注意!!!!")
        }
        LocalStorage.clear();
    }
}
